﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CollegeShop.Infrastructure.Entities.Entity;
using CollegeShop.Infrastructure.Entities;
using CollegeShop.Infrastructure.Component.Cache;

namespace CollegeShop.Biz
{
    public class OrderBiz
    {
        public static List<Order> LoadOrders(string username, int page, int pageSize)
        {
            return null;
        }
        public static ItemListBox<Orderdetail> LoadOrderDetails(string username, int page, int pageSize)
        {
            using (Context ctx = new Context())
            {
                var expression = (from c in ctx.Orderdetail
                                  join o in ctx.Order
                                      on c.OrderID equals o.ID
                                  join g in ctx.Goods
                                      on c.GoodsID equals g.ID
                                  join s in ctx.Resource
                                      on g.ResourceId equals s.ID
                                  where o.BuyUser == username
                                  orderby c.ID descending
                                  select new { c, o, g,s});
                var data = expression.Skip(page * pageSize)
                            .Take(pageSize)
                            .ToList()
                            .Select(c => new Orderdetail().Set(c.c)
                                .LoadOrder(c.o)
                                .LoadGoods(c.g,c.s))
                            .ToList();
                return new ItemListBox<Orderdetail>(data).BuildPage(
                    expression.Count(), page, pageSize, new PageParameter() { Style = "Home" });
            }
        }
    }
}
